package org.kernely.security.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.kernely.persistence.service.SessionService;
import org.kernely.security.persistence.User;
import org.osgi.service.component.ComponentContext;

/**
 * Provides methods to manipulate Users
 */
public class UserDAO {
	/**
	 * SessionService service
	 */
	private static SessionService service;

	/**
	 * The user dao instance
	 */
	private static UserDAO instance;

	/**
	 * Store the user dao instance 
	 * @return the user dao
	 */
	public static UserDAO getInstance() {
		return instance;
	}
	/**
	 * Set the persistence session service
	 * @param sessionService the session service to set
	 */
	public final void setPersistenceService(SessionService sessionService) {
		service = sessionService;
	}

	/**
	 * Activate the component
	 * @param ctxt the activation context
	 */
	protected final void activate(ComponentContext ctxt) {
		instance =this;
	}


	/**
	 * Retrieve the password of a given user
	 * 
	 * @param username
	 *            : the user which password is needed
	 * @return String : the user's password
	 */
	public final String getUserPassword(String username) {
		Session session = service.getSession();
		Criteria crit = session.createCriteria(User.class);
		// Retrieve only user's password specified
		crit.add(Restrictions.like("login", username));
		List list = crit.list();
		if (list == null || list.size() <= 0){
			return null;
		}
		User u = (User) list.get(0);
		return u.getPassword();
	}

	/**
	 * Return all users registered
	 * 
	 * @return a set which contains all users
	 */
	public final Set<User> getUsers() {
		Session session = service.getSession();
		Set<User> set = new HashSet();
		set.addAll(session.createCriteria(User.class).list());
		return set ;
	}

	/**
	 * Return a specific user
	 * 
	 * @param id
	 *            : the id of the user needed
	 * @return the user which have the identifiant id
	 */
	public final User getUser(int id) {
		Session session = service.getSession();
		Criteria crit = session.createCriteria(User.class);
		crit.add(Restrictions.like("id", id));
		List list = crit.list();
		if (list == null || list.size() <= 0){
			return null;
		}
		return (User) list.get(0);
	}

	/**
	 * Return a specific user finded by its username.
	 * 
	 * @param username
	 *            : user's username
	 * @return : the user
	 */
	public final User findUser(String username) {
		Session session = service.getSession();
		Criteria crit = session.createCriteria(User.class);
		crit.add(Restrictions.like("login", username));
		List result = crit.list();
		if (result.size() > 0 ){
			return (User) result.get(0);
		}
		else return null;
	}

	/**
	 * Persist a new user
	 * 
	 * @param user
	 *            : the new user
	 */
	public final void save(User user) {
		Session session = service.getSession();
		session.save(user);
	}

	/**
	 * Update a user
	 * 
	 * @param user
	 *            : the user
	 */
	public final void update(User user) {
		Session session = service.getSession();
		session.merge(user);
	}


	/**
	 * Gets the session service.
	 * 
	 * @return the session service
	 */
	public static SessionService getService() {
		return service;
	}

	/**
	 * Sets the session service.
	 * 
	 * @param service
	 *            the session service to set
	 */
	public static void setService(SessionService service) {
		UserDAO.service = service;
	}

}
